/*
 *  Copyright 2015 The WebRTC Project Authors. All rights reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

package com.cloudptt.wp.ui.call.group;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudptt.api.product.Product;
import com.cloudptt.api.product.ProductConst;
import com.cloudptt.api.product.func.call.CallConst;
import com.cloudptt.api.product.func.call.group.fsm.GroupCallConferenceListener;
import com.cloudptt.api.product.func.call.group.fsm.video.GroupCallMemberInfo;
import com.cloudptt.api.product.func.call.single.LaunchCallInfo;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.api.product.operator.AddrOperator;
import com.cloudptt.wp.R;
import com.cloudptt.wp.SysApplication;
import com.cloudptt.wp.ui.call.group.adapter.GroupCallMemberAdapter;
import com.cloudptt.wp.ui.dlg.GroupCallMemberDlg;
import com.mcptt.db.model.AddrInfo;
import com.mcptt.db.model.GroupMembInfo;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

/**
 * Fragment for call control.
 */
public class GroupCallFragment extends Fragment implements View.OnClickListener, AdapterView.OnItemClickListener, GroupCallConferenceListener {
	private Context ctx = null;

	private LinearLayout recvInfoLy = null;
	private LinearLayout recvNameLy = null;
	private TextView recvNameTv = null;

	private LinearLayout controlLy = null;
	private View controlView;
	private RelativeLayout acceptLy;
	private RelativeLayout disconnectLy;
	private ImageButton acceptButton;
	private ImageButton disconnectButton;

	private RelativeLayout micLy;
	private ImageButton micImageBtn = null;
	private TextView micTv = null;

	private RelativeLayout speakerLy;
	private ImageButton speakerImageBtn = null;
	private TextView speakerTv = null;

	private boolean enableMic = true;
	private boolean enableSpeaker = false;

	private RelativeLayout infoLy = null;
	private ImageButton infoBtn = null;
	private TextView infoBtnTv = null;

	private RelativeLayout transLy = null;
	private ImageButton transBtn = null;
	private TextView transBtnTv = null;
	private RelativeLayout switchLy = null;
	private ImageButton switchBtn = null;
	private TextView switchBtnTv = null;
	private RelativeLayout recvLy = null;
	private ImageButton recvBtn = null;
	private TextView recvBtnTv = null;
	private OnCallEvents callEvents;
	private LaunchCallInfo callInfo = null;
	private AddrInfo peerAddrInfo = null;
	private int curCallState = CallConst.CallState_InitState;
	private boolean isTransBegin = false;
	private boolean isRecvBegin = false;

	private List<GroupCallMemberInfo> newGroupCallMemberInfoList = new Vector<>();
	private List<GroupCallMemberInfo> groupCallMemberInfoList = new Vector<>();
	private List<GroupMembInfo> membList = null;
//	private Map<String, Boolean> conferenceInfoMap = null;

	private GroupCallMemberAdapter groupMemberAdapter = null;
	private boolean isFull = false;
	private boolean isAddMemb = false;

	private GroupCallMemberDlg dlg = null;

	private boolean isActive = false;

	public GroupCallFragment(Context ctx, LaunchCallInfo callInfo,AddrInfo peerAddrInfo,OnCallEvents callEvents)
	{
		this.ctx = ctx;
		this.callInfo = callInfo;
		this.peerAddrInfo = peerAddrInfo;
		this.callEvents = callEvents;
		if(peerAddrInfo != null
				&&(peerAddrInfo.isEmergencyNum()
				|| peerAddrInfo.isCall210()))
		{
			isAddMemb = true;
		}
		initSelfInfo();
	}

	/**
	 * Call control interface for container activity.
	 */
	public interface OnCallEvents {
		public void doUserAction(int btnAction,String infoStr);
		public boolean setSpeakerphoneEnable(boolean isOn);
		public boolean setMicrophoneEnable(boolean isOn);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
							 Bundle savedInstanceState) {
		Log.debug(this.getClass(), "onCreateView");
		controlView =
				inflater.inflate(R.layout.fragment_call_group, container, false);

//		selfLy = (GroupCallSelfLy) controlView.findViewById(R.id.selfLy);
//		if(selfInfo != null)
//		{
//			selfLy.updateSelfGroupCallMemberInfo(selfInfo);
//		}
//		memberListView = (ListView) controlView.findViewById(R.id.memberListView);
//		adapter = new GroupCallMemberListAdapter(ctx, groupCallMemberInfoList,callEvents);
//		memberListView.setAdapter(adapter);
//		memberListView.setOnItemClickListener(this);
		recvInfoLy =
				(LinearLayout) controlView.findViewById(R.id.recvInfoLy);
		recvNameLy =
				(LinearLayout) controlView.findViewById(R.id.recvNameLy);
		recvNameTv = (TextView) controlView.findViewById(R.id.recvNameTv);

		controlLy =
				(LinearLayout) controlView.findViewById(R.id.controlLy);
		infoLy =
				(RelativeLayout) controlView.findViewById(R.id.infoLy);
		infoBtn =
				(ImageButton) controlView.findViewById(R.id.infoBtn);
		infoBtnTv =
				(TextView) controlView.findViewById(R.id.infoBtnTv);
		infoLy.setOnClickListener(this);
		infoBtn.setOnClickListener(this);
		infoBtnTv.setOnClickListener(this);

		transLy =
				(RelativeLayout) controlView.findViewById(R.id.transLy);
		transBtn =
				(ImageButton) controlView.findViewById(R.id.transBtn);
		transBtnTv =
				(TextView) controlView.findViewById(R.id.transBtnTv);
		transLy.setOnClickListener(this);
		transBtn.setOnClickListener(this);
		transBtnTv.setOnClickListener(this);

		switchLy =
				(RelativeLayout) controlView.findViewById(R.id.switchLy);
		switchBtn =
				(ImageButton) controlView.findViewById(R.id.switchBtn);
		switchBtnTv =
				(TextView) controlView.findViewById(R.id.switchBtnTv);
		switchLy.setOnClickListener(this);
		switchBtn.setOnClickListener(this);
		switchBtnTv.setOnClickListener(this);



		recvLy =
				(RelativeLayout) controlView.findViewById(R.id.recvLy);
		recvBtn =
				(ImageButton) controlView.findViewById(R.id.recvBtn);
		recvBtnTv =
				(TextView) controlView.findViewById(R.id.recvBtnTv);

		recvLy.setOnClickListener(this);
		recvBtn.setOnClickListener(this);
		recvBtnTv.setOnClickListener(this);


		acceptLy =
				(RelativeLayout) controlView.findViewById(R.id.acceptLy);
		acceptButton =
				(ImageButton) controlView.findViewById(R.id.acceptBtn);
		acceptButton.setOnClickListener(this);

		disconnectLy =
				(RelativeLayout) controlView.findViewById(R.id.disconnectLy);
		disconnectButton =
				(ImageButton) controlView.findViewById(R.id.disconnectBtn);
		disconnectButton.setOnClickListener(this);
		disconnectButton.setEnabled(false);


		micLy =
				(RelativeLayout) controlView.findViewById(R.id.micLy);
		micLy.setOnClickListener(this);
		micImageBtn =
				(ImageButton) controlView.findViewById(R.id.micImageBtn);
		micImageBtn.setOnClickListener(this);
		micTv =
				(TextView) controlView.findViewById(R.id.micTv);
		micTv.setOnClickListener(this);
		micLy.setVisibility(View.GONE);
		enableMicImageBtn(enableMic);

		speakerLy = (RelativeLayout) controlView.findViewById(R.id.speakerLy);
		speakerLy.setOnClickListener(this);
		speakerImageBtn =
				(ImageButton) controlView.findViewById(R.id.speakerImageBtn);
		speakerImageBtn.setOnClickListener(this);
		speakerTv =
				(TextView) controlView.findViewById(R.id.speakerTv);
		speakerTv.setOnClickListener(this);
		speakerLy.setVisibility(View.GONE);
		enableSpeakerImageBtn(enableSpeaker);
//		mCameraRLayout =
//				(RelativeLayout) controlView.findViewById(R.id.rl_switch_camera);
//		cameraSwitchButton =
//				(ImageButton) controlView.findViewById(R.id.button_call_switch_camera);
//		if(callInfo != null
//				&& callInfo.isHasVideo())
//		{
//			cameraSwitchButton.setVisibility(View.VISIBLE);
//		}
//		else
//		{
//			cameraSwitchButton.setVisibility(View.INVISIBLE);
//		}
//
//		enableMicIb =
//				(ImageButton) controlView.findViewById(R.id.enableMicIb);
//		enableMicIb.setEnabled(enableMic);
//		enableMicIb.setVisibility(View.INVISIBLE);
//
//		enableSpeakerIb =
//				(ImageButton) controlView.findViewById(R.id.enableSpeakerIb);
//		enableSpeakerIb.setEnabled(enableSpeaker);
//		 Add buttons click events.



		return controlView;
	}

	private void initSelfInfo()
	{
		SysApplication app = ((SysApplication) ctx.getApplicationContext());
		Product product = app.getProduct();
//		AddrOperator op = (AddrOperator)product.getOperator(ProductConst.Func_Addr);
//		AddrInfo selfAddrInfo= product.getSysConfig().getMcInfo().toAddrInfo();
//		selfInfo = new GroupCallMemberInfo(selfAddrInfo,"");
		AddrOperator op = (AddrOperator)product.getOperator(ProductConst.Func_Addr);
		String selfAddrId = product.getSysConfig().getMcInfo().getAddrId();
		membList = op.getGroupMemberList(peerAddrInfo,selfAddrId);
		Log.debug(this.getClass()," initSelfInfo membList = " + membList);
		product.setGCallConferenceListener(this);
	}

	@Override
	public void onClick(View v) {
		Log.debug(this.getClass(), "onClick v.getId() = " + v.getId());
		v.setEnabled(false); // 立即禁用按钮
		// 500ms 后恢复可用
		v.postDelayed(() -> v.setEnabled(true), 2000);

		if(R.id.acceptBtn == v.getId())
		{
			callEvents.doUserAction(CallConst.BtnAction_Answer,"");
		}
		else if(R.id.disconnectBtn == v.getId())
		{
			callEvents.doUserAction(CallConst.BtnAction_Hangup,"");
		}
		else if (R.id.micLy == v.getId()
				|| R.id.micImageBtn == v.getId()) {
			Log.debug(this.getClass(), "onClick begin enableMic = " + enableMic);
			enableMic = callEvents.setMicrophoneEnable(!enableMic);
			Log.debug(this.getClass(), "onClick end enableMic = " + enableMic);
			enableMicImageBtn(enableMic);
		}
		else if (R.id.speakerLy == v.getId()
				|| R.id.speakerImageBtn == v.getId()) {
			Log.debug(this.getClass(), "onClick begin enableSpeaker = " + enableSpeaker);
			enableSpeaker = !enableSpeaker;
			callEvents.setSpeakerphoneEnable(enableSpeaker);
			Log.debug(this.getClass(), "onClick end enableSpeaker = " + enableSpeaker);
			enableSpeakerImageBtn(enableSpeaker);
		}
		else if(R.id.transLy == v.getId()
				|| R.id.transBtn == v.getId()
				|| R.id.transBtnTv == v.getId())
		{
			if(!isTransBegin)
			{
				callEvents.doUserAction(CallConst.BtnAction_Trans_Begin,"");
				transBtn.setEnabled(false);

//				isTransBegin = true;
			}
			else
			{
				callEvents.doUserAction(CallConst.BtnAction_Trans_End,"");
				transBtn.setEnabled(false);
//				transBtn.setPressed(false);
//				isTransBegin = false;
			}
		}
		else if(R.id.recvLy == v.getId()
				|| R.id.recvBtn == v.getId()
				|| R.id.recvBtnTv == v.getId())
		{
			if (groupMemberAdapter != null) {
				int position = groupMemberAdapter.getSelectedPos();
				Log.debug(this.getClass(), "onClick recvBtn position = " + position);
				if (groupCallMemberInfoList != null
						&& groupCallMemberInfoList.size() > position
						&& groupCallMemberInfoList.get(position) != null)
				{
					GroupCallMemberInfo info = groupCallMemberInfoList.get(position);
					Log.debug(this.getClass(), "onClick recvBtn info = " + info);
					if(info.isTrans()) {
						String jsonStr = info.getJsonStr();
//						if (!isRecvBegin) {
//							callEvents.doUserAction(CallConst.BtnAction_Recv_Begin, jsonStr);
//							isRecvBegin = true;
//						} else
						if (isRecvBegin)
						{
							callEvents.doUserAction(CallConst.BtnAction_Recv_End, jsonStr);
							recvBtn.setEnabled(false);
							recvBtn.setImageResource(R.drawable.btn_group_call_video_picture_pressed);
//							recvBtn.setPressed(false);
//							transBtn.invalidate();
//							isRecvBegin = false;
						}
					}
				}
			}
		}
		else if(R.id.switchLy == v.getId()
				|| R.id.switchBtn == v.getId()
				|| R.id.switchBtnTv == v.getId())
		{
			callEvents.doUserAction(CallConst.BtnAction_SwitchCamera,"");
		}
		else if(R.id.infoLy == v.getId()
				|| R.id.infoBtn == v.getId()
				|| R.id.infoBtnTv == v.getId())
		{
			doDetail();
		}
	}

	public void doDetail()
	{
		Log.debug(this.getClass(), "doDetail membListBtn peerAddrInfo = " + peerAddrInfo);
		SysApplication app = ((SysApplication) ctx.getApplicationContext());
		Product product = app.getProduct();
		product.getGCallEventAdapter().subcribeConference();
//			AddrOperator op = (AddrOperator)product.getOperator(ProductConst.Func_Addr);
//			String selfAddrId = product.getSysConfig().getMcInfo().getAddrId();
//			membList = op.getGroupMemberList(peerAddrInfo,selfAddrId);
//			Log.debug(this.getClass()," onCreate membList = " + membList);
		updateNewGroupCallMemberInfoList();
		if(groupMemberAdapter == null) {
			groupMemberAdapter = new GroupCallMemberAdapter(ctx, newGroupCallMemberInfoList, callEvents);
		}
		else
		{
			groupMemberAdapter.refresh(newGroupCallMemberInfoList);
		}
		dlg = new GroupCallMemberDlg(ctx,groupMemberAdapter);//UiDlg.getAdapterListDialog(ctx,groupMemberAdapter);
		dlg.show();
	}

	@Override
	public void onStart() {
		super.onStart();
		Log.debug(this.getClass(), "onStart callInfo = " + callInfo);
		boolean captureSliderEnabled = false;
		//    String contactName = args.getString(CallActivity.EXTRA_IP);
		//    contactView.setText(contactName);
		//    videoCallEnabled = args.getBoolean(CallActivity.EXTRA_VIDEO_CALL, true);
		//    videoCallEnabled = para.isVideoCallEnabled();
		captureSliderEnabled = false;//videoCallEnabled && para.isCaptureQualitySlider();
//		if (!videoCallEnabled) {
//			cameraSwitchButton.setVisibility(View.INVISIBLE);
//		}
		if (captureSliderEnabled) {
//			captureFormatSlider.setOnSeekBarChangeListener(
//					new CaptureQualityController(captureFormatText, callEvents));
		}
		int callState = callInfo.getCallState();
		Log.debug(this.getClass(), "onStart callState = " + callState);
		updateView(callState);
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		callEvents = (OnCallEvents) activity;
	}

	public void updateTransState(int state)
	{
		Log.debug(this.getClass(), "updateTransState state = " + state);
		if(CallConst.VideoTransState_NoPermission == state)
		{
			isTransBegin = false;
			transBtnTv.setText(R.string.group_call_trans_begin);
			transBtn.setImageResource(R.drawable.btn_group_call_video_camera_pressed);
			transBtn.setEnabled(true);
			switchLy.setVisibility(View.GONE);
		}
		else if(CallConst.VideoTransState_Permission == state)
		{
			isTransBegin = true;
			transBtnTv.setText(R.string.group_call_trans_end);
			transBtn.setImageResource(R.drawable.btn_group_call_video_camera_normal);
			transBtn.setEnabled(true);
			switchLy.setVisibility(View.VISIBLE);
		}
		Log.debug(this.getClass(), "updateTransState state = " + state + " isTransBegin = "  + isTransBegin + " isRecvBegin = " + isRecvBegin);
//		if(selfLy != null) {
//			selfInfo.update(isTransBegin,isRecvBegin);
////			selfLy.updateSelfGroupCallMemberInfo(selfInfo);
//			selfLy.updateTransState(state);
//		}
	}

	public void updateRecvState(int state)
	{
		Log.debug(this.getClass(), "updateRecvState state = " + state);
		if(CallConst.VideoRecvState_NoPermission == state)
		{
			isRecvBegin = false;
			recvNameLy.setVisibility(View.GONE);
			recvLy.setVisibility(View.GONE);
			if(groupMemberAdapter != null)
			{
				groupMemberAdapter.setIsView(false);
			}
		}
		else if(CallConst.VideoRecvState_Permission == state)
		{
			recvBtn.setEnabled(true);
			isRecvBegin = true;
			recvNameLy.setVisibility(View.VISIBLE);
			GroupCallMemberInfo info= groupMemberAdapter.getSelectedOb();
			if(info != null)
			{
				if (info.getGroupMembInfo() != null)
				{
					String dscp = info.getGroupMembInfo().getDscp();
					recvNameTv.setText(dscp);
				}
			}

			recvLy.setVisibility(View.VISIBLE);
			recvBtnTv.setText(R.string.group_call_recv_end);
			recvBtn.setImageResource(R.drawable.btn_group_call_video_picture_normal);
			Log.debug(this.getClass(), "updateRecvState isRecvBegin = " + isRecvBegin);
			if(groupMemberAdapter != null)
			{
				groupMemberAdapter.setIsView(true);
			}
		}
		Log.debug(this.getClass(), "updateRecvState state = " + state + " isTransBegin = "  + isTransBegin + " isRecvBegin = " + isRecvBegin);
//		if(selfLy != null) {
//			selfInfo.update(isTransBegin,isRecvBegin);
////			selfLy.updateSelfGroupCallMemberInfo(selfInfo);
//			selfLy.updateRecvState(state);
//		}
	}

	public void updateGroupCallMemberInfoList(List<GroupCallMemberInfo> groupCallMemberInfoList)
	{
		Log.debug(this.getClass(), "updateGroupCallMemberInfoList groupCallMemberInfoList = " + groupCallMemberInfoList);
		this.groupCallMemberInfoList = groupCallMemberInfoList;

		new Handler(Looper.getMainLooper()).post(() -> {
			if(groupMemberAdapter != null) {
				updateNewGroupCallMemberInfoList();
				groupMemberAdapter.refresh(newGroupCallMemberInfoList);
				groupMemberAdapter.notifyDataSetChanged();
			}
		});
	}
	@Override
	public void updateConferenceInfoList(String groupAddrId, boolean isFull,Map<String, Boolean> conferenceInfoMap) {
		Log.debug(this.getClass(),"updateConferenceInfoList groupAddrId =  "+ groupAddrId +" isFull = "+isFull +  " conferenceInfoMap = " + conferenceInfoMap);

		if(isAddMemb
				&& conferenceInfoMap != null
				&& conferenceInfoMap.size() > 0)
		{
			Set<String> keySet = conferenceInfoMap.keySet();
			for(Iterator<String> itor = keySet.iterator();itor.hasNext();)
			{
				String num = itor.next();
				groupAddrId = peerAddrInfo.getAddrId();
				String numUrl = AddrInfo.toUrlFromNum(num);
				GroupMembInfo info = new GroupMembInfo(groupAddrId,num,num,numUrl,0);
				String addrId = info.getAddrId();
				if(!memberIsConnect(conferenceInfoMap,addrId))
				{
					membList.add(info);
				}
			}
		}

		if(groupMemberAdapter != null)
		{
			Log.debug(this.getClass(), "updateConferenceInfoList 1 membList = " + membList);
			updateMembStatus(conferenceInfoMap);
			Log.debug(this.getClass(), "updateConferenceInfoList 2 membList = " + membList);
//			Collections.sort(membList);
			Log.debug(this.getClass(), "updateConferenceInfoList 3 membList = " + membList);
			updateNewGroupCallMemberInfoList();
			Log.debug(this.getClass(), "updateConferenceInfoList 4 newGroupCallMemberInfoList = " + newGroupCallMemberInfoList);
			Collections.sort(newGroupCallMemberInfoList);
			groupMemberAdapter.refresh(newGroupCallMemberInfoList);
			groupMemberAdapter.notifyDataSetChanged();
		}
	}


	public void updateView(int callState)
	{
		Log.debug(this.getClass(), "updateView callState = " + callState);
		if(callState == CallConst.CallState_Activite)
		{
			this.viewOnConnected();
			isActive = true;
//			transBtn.setVisibility(View.VISIBLE);
//			recvBtn.setVisibility(View.VISIBLE);
		}
		else if(callState == CallConst.CallState_ConnectingState)
		{
			if(!isActive) {
				if (callInfo.isMoCall()) {
					viewOnCalling();
				} else {
//					viewOnIncoming();
					this.viewOnConnected();
				}
			}
//			transBtn.setVisibility(View.VISIBLE);
//			recvBtn.setVisibility(View.VISIBLE);
			if(dlg != null) {
				dlg.dismiss();
			}
		}
		else if(callState == CallConst.CallState_InitState)
		{
			if(dlg != null) {
				dlg.dismiss();
			}
		}
	}

	public void dismissDlg()
	{
		Log.debug(this.getClass(), "dismissDlg");
		if(dlg != null) {
			dlg.dismiss();
		}
	}
	public void refreshDlg()
	{
		Log.debug(this.getClass(), "refreshDlg");
		if(groupMemberAdapter != null)
		{
			groupMemberAdapter.refresh(newGroupCallMemberInfoList);
			groupMemberAdapter.notifyDataSetChanged();
		}
	}

	public void viewAfterOffer()
	{
		acceptLy.setEnabled(true);

	}
	private void viewOnCalling()
	{
		Log.debug(this.getClass(), "viewOnCalling");
		//acceptButton.setVisibility(View.GONE);
		//disconnectButton.setVisibility(View.VISIBLE);
		//cameraSwitchButton.setVisibility(View.GONE);
		//toggleMuteButton.setVisibility(View.GONE);

		if(acceptLy != null)
		{
			acceptLy.setVisibility(View.GONE);
		}
		if(disconnectLy != null)
		{
			disconnectLy.setVisibility(View.VISIBLE);
		}
//		mCameraRLayout.setVisibility(View.GONE);
//		mToggleMuteRLayout.setVisibility(View.GONE);

	}

	private void viewOnIncoming()
	{
		Log.debug(this.getClass(), "viewOnIncoming");
		/*acceptButton.setVisibility(View.VISIBLE);
	  disconnectButton.setVisibility(View.VISIBLE);
	  cameraSwitchButton.setVisibility(View.GONE);
	  toggleMuteButton.setVisibility(View.GONE);*/
		if(acceptLy != null) {
			acceptLy.setVisibility(View.VISIBLE);
		}
		if(disconnectLy != null) {
			disconnectLy.setVisibility(View.VISIBLE);
		}
//		mCameraRLayout.setVisibility(View.GONE);
//		mToggleMuteRLayout.setVisibility(View.GONE);
	}

	public void viewOnConnected()
	{
		Log.debug(this.getClass(), "viewOnConnected");
		disconnectButton.postDelayed(() -> disconnectButton.setEnabled(true), 500);

		if(acceptLy != null) {
			acceptLy.setVisibility(View.GONE);
		}
		if(disconnectLy != null) {
			disconnectLy.setVisibility(View.VISIBLE);
		}
		transLy.setVisibility(View.VISIBLE);
		micLy.setVisibility(View.VISIBLE);
		speakerLy.setVisibility(View.VISIBLE);
	}

	public void setCallState(LaunchCallInfo callInfo) {
		Log.debug(this.getClass(), "setCallState callInfo = " + callInfo);
		if(callInfo != null)
		{
			this.callInfo = callInfo;
			int callState = callInfo.getCallState();
			Log.debug(this.getClass(), "setCallState callState = " + callState + " curCallState = " + curCallState);
			if(curCallState <= callState) {
				curCallState = callState;
				Log.debug(this.getClass(), "setCallState callState = " + callState);
				updateView(callState);
			}
		}
	}
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//		if(transUserInfoList != null
//			&& transUserInfoList.get(position) != null)
//		{
//			GroupCallMemberInfo info = transUserInfoList.get(position);
//			String jsonStr = info.getJsonStr();
//			callEvents.doUserAction(CallConst.BtnAction_Recv_Begin,jsonStr);
//			isRecvBegin = true;
//		}
		if(groupMemberAdapter != null)
		{
			groupMemberAdapter.setSelectedPos(position);
		}
	}

	private void updateMembStatus(Map<String, Boolean> conferenceInfoMap)
	{
		for (Iterator<GroupMembInfo> itor = membList.iterator(); itor.hasNext();)
		{
			GroupMembInfo membInfo = itor.next();
			String addrId = membInfo.getAddrId();
			boolean isConnected = memberIsConnect(conferenceInfoMap,addrId);
			membInfo.setStatus(isConnected);
		}
	}
	private boolean memberIsConnect(Map<String, Boolean> conferenceInfoMap,String addrId)
	{

		if(conferenceInfoMap != null
				&& conferenceInfoMap.get(addrId) != null)
		{
			return conferenceInfoMap.get(addrId);
		}
		return false;
	}
	public void updateNewGroupCallMemberInfoList()
	{
		Log.debug(this.getClass(),"updateNewGroupCallMemberInfoList membList = " + membList);
		newGroupCallMemberInfoList.clear();
		if(groupCallMemberInfoList != null
				&& groupCallMemberInfoList.size() > 0)
		{
			newGroupCallMemberInfoList.addAll(groupCallMemberInfoList);
		}
		for (Iterator<GroupMembInfo> itor = membList.iterator(); itor.hasNext();)
		{
			GroupMembInfo membInfo = itor.next();
			Log.debug(this.getClass(),"updateNewGroupCallMemberInfoList membInfo =  "+ membInfo);
			boolean isHasGroupCallMemberInfo = isHasGroupCallMemberInfo(membInfo);
			Log.debug(this.getClass(),"updateNewGroupCallMemberInfoList isHasGroupCallMemberInfo =  "+ isHasGroupCallMemberInfo);
			if(!isHasGroupCallMemberInfo)
			{
				GroupCallMemberInfo info = new GroupCallMemberInfo(membInfo,"");
				newGroupCallMemberInfoList.add(info);
			}
		}
	}
	private boolean isHasGroupCallMemberInfo(GroupMembInfo info )
	{
		Log.debug(this.getClass(),"isHasGroupCallMemberInfo info =  "+ info +" membList = " + membList);
		for (Iterator<GroupCallMemberInfo> itor = groupCallMemberInfoList.iterator(); itor.hasNext();)
		{
			GroupCallMemberInfo membInfo = itor.next();
			String addrId = info.getAddrId();
			if(membInfo != null
					&& membInfo.isSameOne(addrId))
			{
//				int status = info.getStatus();
//				membInfo.getGroupMembInfo().setStatus(status);
				return true;
			}
		}
		return false;
	}

	public void  enableMicImageBtn(boolean isEnable)
	{
		if(isEnable)
		{
			micTv.setText(R.string.call_btn_mic_on);
			micImageBtn.setImageResource(R.drawable.btn_call_mic_normal);
		}
		else
		{
			micTv.setText(R.string.call_btn_mic_off);
			micImageBtn.setImageResource(R.drawable.btn_call_mic_pressed);
		}
	}
	public void enableSpeakerImageBtn(boolean isEnable)
	{
		if(enableSpeaker)
		{
			speakerTv.setText(R.string.call_btn_speaker_on);
			speakerImageBtn.setImageResource(R.drawable.btn_call_speaker_normal);
		}
		else
		{
			speakerTv.setText(R.string.call_btn_speaker_off);
			speakerImageBtn.setImageResource(R.drawable.btn_call_speaker_pressed);
		}
	}
	public void hideControlUi(boolean isShow)
	{
		if(isShow)
		{
			recvInfoLy.setVisibility(View.VISIBLE);
			controlLy.setVisibility(View.VISIBLE);
		}
		else {
			recvInfoLy.setVisibility(View.INVISIBLE);
			controlLy.setVisibility(View.INVISIBLE);
		}
	}
}
